<template>
    <!-- 新增楼层 -->
    <el-dialog v-model="isShow" title="新增楼层库位码" width="527">
        <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
            <el-form-item label="楼层" prop="floor">
                <el-input-number v-model="formData.floor" style="width: 214px;" min="1" max="10" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="resetForm(ruleFormRef)">取消</el-button>
                <el-button type="primary" v-loading="loading" @click="submitForm(ruleFormRef)">确定</el-button>
            </span>
        </template>
    </el-dialog>

</template>
<script setup lang="ts">
import { ref, defineExpose, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { stockLocationGenerate } from '@/api/depot/index'
import { addData } from '@/api/system/dict/data/index.ts'
const emit = defineEmits(['getTreeData'])

const loading = ref(false)
const isShow = ref(false)
const ruleFormRef = ref()
const formData = ref({
    floor: 1,
})
const paramsArr = [
    {
        dictLabel: "楼层一(L1)",
        dictValue: "L1",
    },
    {
        dictLabel: "楼层二(L2)",
        dictValue: "L2",
    },
    {
        dictLabel: "楼层三(L3)",
        dictValue: "L3",
    },
    {
        dictLabel: "楼层四(L4)",
        dictValue: "L4",
    },
    {
        dictLabel: "楼层五(L5)",
        dictValue: "L5",
    },
    {
        dictLabel: "楼层六(L6)",
        dictValue: "L6",
    },
    {
        dictLabel: "楼层七(L7)",
        dictValue: "L7",
    },
    {
        dictLabel: "楼层八(L8)",
        dictValue: "L8",
    },
    {
        dictLabel: "楼层九(L9)",
        dictValue: "L9",
    },
    {
        dictLabel: "楼层十(L10)",
        dictValue: "L10",
    },
]
// 只能输入1到99之间的整数
const validatePass = (rule: any, value: string, callback: Function) => {
    if (value) {
        return /^([1-9]|[1-9]\d)$/.test(value)
    } else {
        callback()
    }
}
// 表单验证
const rules = reactive(
    {
        floor: [
            {
                required: true,
                message: '请输入楼层数量',
                trigger: 'change',
            }, {
                validator: validatePass,
                message: '请输入1-10的整数',
                trigger: 'change'
            }
        ]
    }
)
// 确定操作
const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            apiExecute(formEl)
        } else {
            console.log('error submit!', fields)
        }
    })
}
// 表单重置
const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
    isShow.value = false
}
const apiExecute = async (formEl: any) => {
    loading.value = true
    try {
        const params = {
            dictCode: undefined,
            dictLabel: "",
            dictValue: "",
            cssClass: "",
            listClass: "default",
            dictSort: 0,
            remark: "",
            dictType: "hm_stock_location_floor"
        }
        paramsArr.forEach((item, index) => {
            if (formData.value.floor == index + 1) {
                params.dictLabel = item.dictLabel
                params.dictValue = item.dictValue
                params.dictSort = index + 1
            }
        })
        const { code = 200 } = await addData(params)
        loading.value = false
        if (code == 200) {
            ElMessage({
                message: '新增楼层成功',
                type: 'success',
            })
            emit('getTreeData')
            resetForm(formEl)
        } else {
            ElMessage({
                message: '新增楼层失败',
                type: 'error',
            })
        }
    } catch (error) {
        loading.value = false
    }
}

defineExpose({ formData, isShow })

</script>
<style lang="scss" scoped>
@import url(../style/index.scss);
</style>